package com.mdd.service.pay.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.mdd.service.pay.AlipayAuthTradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * {@code @Description}
 * @Author 幻速
 * {@code @Date} 2023/10/30 17:42
 **/
@Slf4j
@Service
public class AlipayAuthTradeServiceImpl implements AlipayAuthTradeService {
    /**
     * 支付接口（代扣/预授权扣款）
     * @param alipayClient 请求对象
     * @param alipayTradePayModel 支付参数（代扣/预授权扣款）
     * @return 返回支付结果
     * @throws AlipayApiException 支付异常
     */
    @Override
    public AlipayTradePayResponse tradePay(AlipayClient alipayClient, AlipayTradePayModel alipayTradePayModel) throws AlipayApiException {
        log.info("AlipayTradePayRequest   params----->"+alipayTradePayModel);
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        request.setBizModel(alipayTradePayModel);
        AlipayTradePayResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradePayRequest-------->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 收单交易关闭
     * @param alipayClient 请求对象
     * @param tradeCloseModel 关闭预授权参数
     * @param notifyUrl 收单交易通知
     */
    @Override
    public AlipayTradeCloseResponse tradeClose(AlipayClient alipayClient, AlipayTradeCloseModel tradeCloseModel, String notifyUrl) throws AlipayApiException {
        log.info("AlipayTradeCloseRequest  params------->"+JSON.toJSONString(tradeCloseModel));
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        request.setNotifyUrl(notifyUrl);
        request.setBizModel(tradeCloseModel);
        AlipayTradeCloseResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradeCloseResponse------->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 交易查询
     * @param alipayClient 请求对象
     * @param tradeQueryModel 交易查询
     * @param notifyUrl 交易通知
     * @return 查询交易结果
     * @throws AlipayApiException 查询异常
     */
    @Override
    public AlipayTradeQueryResponse tradeQuery(AlipayClient alipayClient, AlipayTradeQueryModel tradeQueryModel, String notifyUrl) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        log.info("AlipayTradeQueryRequest   params------>"+JSON.toJSONString(tradeQueryModel));
        request.setNotifyUrl(notifyUrl);
        request.setBizModel(tradeQueryModel);
        AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradeQueryResponse----->"+JSON.toJSONString(response));
        return response;
    }

}
